<?php
namespace App\Service\Xcx\Ywmall;

use App\Service\Xcx\BaseService as Service;

class ShopService extends Service
{
    //服务

    //获取用户的店铺列表
    public static function shopidsByUid($uid)
    {
        $res = self::dbConn(self::$dbUserShop)
            ->where('del', 0)
            ->where('uid', $uid)
            ->get();
        if (!count($res)) return [];
        $arys = [];
        foreach ($res as $v) {
            $arys[] = $v->shop_id;
        }
        return $arys;
    }

    //列表
    public static function index($pages, $uid)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwShop)->where('del', 0);
        $shopids = self::shopidsByUid($uid);
        if (!count($shopids)) {
            return self::toback(-2, '没有店铺');
        }
        $db = $db->whereIn('id', $shopids);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据', [], $total);
        }
        $ary = [];
        foreach ($res as $v) {
            $ary[] = [
                'id' => $v->id, 'title' => $v->title,
                //'head' => file_exists($v->head) ? env('DOMAIN') . '/'. $v->head : '',
                'head' => $v->head,
            ];
        }
        return self::toback(0, '', $ary, $total);
    }

    //新增
    public static function add($pas, $uid)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $shop_id = self::dbConn(self::$dbYwShop)->insertGetId($pas);
        $us = self::dbConn(self::$dbUserShop)
            ->where('del', 0)
            ->where('leader', 3)
            ->where('shop_id', $shop_id)
            ->where('uid', $uid)
            ->first();
        if (empty($us)) {
            self::dbConn(self::$dbUserShop)->insertGetId([
                'shop_id' => $shop_id, 'uid' => $uid,
                'leader' => 3, 'addtime' => toDate(),
            ]);
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas, $uid)
    {
        $res = self::dbConn(self::$dbYwShop)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwShop)
            ->where('id', $id)
            ->update($pas);
        if (empty($us)) {
            self::dbConn(self::$dbUserShop)->insertGetId([
                'shop_id' => $id, 'uid' => $uid,
                'leader' => 3, 'addtime' => toDate(),
            ]);
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $attrOne = self::dbConn(self::$dbYwShop)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($attrOne)) {
            return self::toback(-2, '没有店铺');
        }
        $up = self::dbConn(self::$dbYwShop)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }
}